<template>
	<view class="content">
		<view class="head">
			<u-navbar leftIcon="arrow-leftward" :placeholder='true' title="確認訂單"
				@leftClick="leftClick('/pages/shopping/index')">
			</u-navbar>
		</view>
		<view class="main">
			<view class="address">
				<view class="a-l">
					收货地址
				</view>
				<view class="a-r">
					<!-- <u-icon size="28rpx" name="arrow-right"></u-icon> -->
				</view>
			</view>
			<view class="address">
				<view class="a-l" v-if="addreslist[addressindex]">
					<view class="top">
						<text class="name">{{addreslist[addressindex].consignee}}</text>
						<text class="phone">{{addreslist[addressindex].mobile}}</text>
					</view>
					<view class="bottom">
						{{addreslist[addressindex].address}}
					</view>
				</view>
				<view class="a-r">
					<picker @change="addressChange" :value="addressindex" :range="addreslist" range-key="address">
						<view class="picker" style="width: 60rpx;display: flex;justify-content: end;">
							<!-- <view class="uni-input">{{addreslist[addressindex]}}</view> -->
							<u-icon size="28rpx" name="arrow-right"></u-icon>
						</view>
					</picker>
				</view>
			</view>
			<view class="address">
				<view class="a-l">
					選擇部門
				</view>
				<view class="a-r">
					<picker @change="memberChange" :value="memberindex" :range="memberlist" range-key="departmentname">
						<view class="picker" style="display: flex;align-items: center;">
							<view class="uni-input" v-if="memberlist[memberindex]">
								{{memberlist[memberindex].departmentname}}
							</view>
							<u-icon size="28rpx" name="arrow-right"></u-icon>
						</view>
					</picker>
				</view>
			</view>
			<view class="goods">
				<view class="good-item" v-for="(item,index) in cartlist" :key="index" @click="$store.dispatch('toDetails',item.goods_id)" >
					<view class="g-l">
						<image class="g-l-img" :src="getFirstImageLink(item.goods_images)
						" mode="aspectFit"></image>
					</view>
					<view class="g-r">
						<view class="r-top">
							{{item.goods_name}}
						</view>
						<view class="r-center">
							藍色 | 數量：{{item.goods_number}}
						</view>
						<view class="r-bottom">
							MOP {{item.goods_price}}
						</view>
					</view>
				</view>
			</view>
			<view class="address">
				<view class="a-l">
					備註
				</view>
				<view class="a-input">
					<input v-model="submitfrom.remark" type="text" placeholder="50漢字內" name="" id="">
				</view>
			</view>
			<view style="height: 200rpx;"></view>
		</view>
		<view class="end">
			<view class="end-left">

			</view>
			<view class="end-right">
				<view class="end-price">
					<view>
						共{{totalNum}}件，總計：<text
							style="font-size: 30rpx;font-weight: bold;color: #4949C3;">MOP{{totalPrice}}</text>
					</view>
					<view style="font-size: 22rpx;font-weight: 500;color: #333333;">
						運費：MOP 0
					</view>
				</view>
				<view class="btn" @click="Tosubmit">
					提交
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAddresslist,
		getmember,
		submitcart,
		orderstatus
	} from '@/network/api/cart.js'
	export default {
		data() {
			return {
				addressindex: 0,
				memberindex: 0,
				showaddress: false,
				showmember: false,
				cartlist: [],
				totalPrice: '',
				totalNum: '',
				memberlist: [],
				addreslist: [],
				submitfrom: {
					department_id: '',
					remark: '',
					consignee: '',
					address: '',
					mobile: '',
					email: '',
				}
			}
		},
		onLoad(e) {
			this.cartlist = JSON.parse(e.cartlist);
			this.totalPrice = e.totalPrice
			this.totalNum = e.totalNum
			this.getAddress()
			this.getmemberlist()
		},
		methods: {
			async Tosubmit() {
				this.submitfrom.department_id = this.memberlist[this.memberindex] ? this.memberlist[this.memberindex].department_id : ''
				this.submitfrom.consignee = this.addreslist[this.addressindex].consignee
				this.submitfrom.address = this.addreslist[this.addressindex].address
				this.submitfrom.mobile = this.addreslist[this.addressindex].mobile
				this.submitfrom.email = this.addreslist[this.addressindex].email
				let rec_id = this.cartlist.map(item => item.rec_id);
				this.submitfrom.goods = rec_id
				uni.showLoading({
					title: "下單中，請稍後..",
					mask: true,
				});
				const res = await submitcart(this.submitfrom)
				if (res.data.code == 1) {
					uni.hideLoading()
					let order_id=res.data.order_id
					uni.navigateTo({
						url: `/pages/shopping/order/success?totalPrice=${this.totalPrice}&order_id=${order_id}`
					})
				}else if(res.data.code == 8){
          uni.showToast({
            title: '當前訂單價格已超低於限定金額！',
            icon: 'none',
            duration: 2000
          })
        }else if (res.data.code == 4){
          uni.hideLoading()
        }else if (res.data.code == 2){
          uni.showToast({
            title: '商品庫存不足，請聯繫管理員',
            icon: 'none',
            duration: 2000
          })

        }else if (res.data.code == 3) {
          uni.hideLoading()
        }
          console.log(this.cartlist);
          // 'goods[0]': '',
          console.log(this.submitfrom);

        },
			memberChange(e) {
				this.memberindex = e.detail.value
			},
			addressChange(e) {
				this.addressindex = e.detail.value
			},
			async getAddress() {
				let data = {
					user_id: uni.getStorageSync('user').userinfo.id
				}
				const res = await getAddresslist(data)
				this.addreslist = res.data.address

			},
			async getmemberlist() {
				let company_id = uni.getStorageSync('user').userinfo.company_id
				const res = await getmember(company_id)
				this.memberlist = res.data.department
			},

			leftClick(url) {
				uni.switchTab({
					url: url
				})
			}
		},
	}
</script>

<style lang="scss">
	.content {
		.main {
			margin: 0 32rpx;

			.address {
				padding: 32rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1rpx solid #DEDEDE;

				.a-input {
					display: flex;
					justify-content: flex-end;
				}

				.a-l {
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #333333;

					.top {
						.name {
							font-size: 28rpx;
							font-weight: bold;
							color: #333333;
							margin-right: 24rpx;
						}

						.phone {
							font-size: 28rpx;
							font-weight: bold;
							color: #333333;
						}
					}

					.bottom {
						font-size: 28rpx;
						font-weight: 500;
						color: #333333;
					}
				}

				.a-r {

				}
			}

			.goods {
				.good-item {
					display: flex;
					padding: 32rpx 0;
					border-bottom: 1rpx solid #DEDEDE;
					;

					.g-l {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 180rpx;
						height: 180rpx;
						background: #FFFFFF;
						border: 1px solid #DEDEDE;
						border-radius: 30rpx;

						.g-l-img {
              max-width: 90%;
              max-height: 90%;
						}
					}

					.g-r {
						margin-left: 30rpx;
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.r-top {
							font-size: 28rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
						}

						.r-center {
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 400;
							line-height: 43rpx;
							color: #999999;
						}

						.r-bottom {
							font-size: 28rpx;
							font-family: PingFang SC;
							font-weight: bold;
							line-height: 43rpx;
							color: #4949C3;
						}
					}
				}
			}
		}

		.end {
			width: 100%;
			height: 110rpx;
			background-color: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			display: flex;
			align-items: center;
			justify-content: space-between;

			&-left {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #333333;
				display: flex;
				justify-content: space-between;
				padding: 0 30rpx;

				.end-flex {
					display: flex;
					align-items: center;
				}
			}

			&-right {
				margin-right: 32rpx;
				display: flex;

				.end-price {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-end;

				}

				.btn {
					margin-left: 20rpx;
					text-align: center;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: bold;
					line-height: 0rpx;
					color: #FFFFFF;
					width: 170rpx;
					height: 80rpx;
					line-height: 80rpx;
					background: #645AED;
					border-radius: 30rpx;
				}

				.red {
					background: #ED0000;
				}

			}
		}

	}
</style>
